package com.atguigu.szdz.dcsyl.controller;

import com.atguigu.szdz.dcsyl.common.utils.R;
import com.atguigu.szdz.dcsyl.service.WellInfoService;
import com.atguigu.szdz.dcsyl.utils.MapperUtils;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/wellInfo")
public class WellInfoController {

    @Autowired
    private WellInfoService wellInfoService;
    @Autowired
    private MapperUtils mapperUtils;

    @PostMapping("/getWellInfoList")
    @Operation(summary = "获取井信息列表")
    public R getWellInfoList(@RequestBody Map<String, Object> map) {
        Integer currentPage = (Integer) map.get("currentPage");
        String keyword = (String) map.get("keyword");
        if ((keyword != "") && (keyword.length() > 0)) {
            keyword
                    = " where (well_name like '%" + keyword + "%')";
        }
        List<Map<String, Object>> wellInfoList = wellInfoService.getWellInfoList(currentPage, keyword);
        //分页参数
        return R.ok().put("wellInfoList", wellInfoList);
    }

    @Operation(summary = "添加井信息")
    @PostMapping("/addWellInfo")
    public R addWellInfo(@RequestBody Map<String, Object> map) {
        wellInfoService.addWellInfo(map);
        return R.ok();
    }

    @Operation(summary = "编辑井信息")
    @PostMapping("/updateWellInfo")
    public R updateWellInfo(@RequestBody Map<String, Object> map) {
        wellInfoService.updateWellInfo(map);
        return R.ok();
    }

    @Operation(summary = "删除井信息")
    @PostMapping("/deleteWellInfo")
    public R deleteWellInfo(@RequestBody Map<String, Object> map) {
        Integer well_id = (Integer) map.get("well_id");
        wellInfoService.deleteWellInfo(well_id);
        return R.ok();
    }

    @Operation(summary = "获取井信息详情")
    @PostMapping("/getWellInfoDetail")
    public R getWellInfoDetail(@RequestBody Map<String, Object> map) {
        Integer well_id = (Integer) map.get("well_id");
        Map<String, Object> wellInfoDetail = wellInfoService.getWellInfoDetail(well_id);
        return R.ok().put("wellInfoDetail", wellInfoDetail);
    }

    @Operation(summary = "根据井id修改nhg数据")
    @PostMapping("/updateNhgByWellId")
    public R updateNhgByWellId(@RequestBody Map<String, Object> map) {
        Integer well_id = (Integer) map.get("well_id");
        //海水密度
        Double Swg = map.get("swg") != null ? Double.parseDouble(map.get("swg").toString()) : 1.025;
        //地层水密度
        Double rhof = map.get("rhof") != null ? Double.parseDouble(map.get("rhof").toString()) : 1.023;
        //水深
        Double Wd = map.get("wd") != null ? Double.parseDouble(map.get("wd").toString()) : 0.0;
        //补心高
        Double Kb = map.get("kb") != null ? Double.parseDouble(map.get("kb").toString()) : 0.0;
        //存放id和新的nhg，方便更新
        List<Map<String, Object>> nhgList = new ArrayList<>();
        List<Map<String, Object>> depthList = wellInfoService.getDepthList(well_id);
        if (depthList != null && depthList.size() > 0) {
            for (Map<String, Object> depthMap : depthList) {
                Double depth = (Double) depthMap.get("depth");
                Integer id = (Integer) depthMap.get("id");
                Double nhg = (Swg * Wd + rhof * (depth - Kb - Wd)) / (depth - Kb);
                depthMap.put("nhg", nhg);
                depthMap.put("id", id);
                nhgList.add(depthMap);
            }
        }
        if (nhgList != null && nhgList.size() > 0) {
            for (Map<String, Object> nhgMap : nhgList) {
                Integer id = (Integer) nhgMap.get("id");
                Double nhg = (Double) nhgMap.get("nhg");
                String sql = "update \"dc_ylDataDc\" set nhg = " + nhg + " where id = " + id;
                mapperUtils.updataBySql(sql);
            }
        }
        return R.ok();
    }

    @Operation(summary = "采用默认的nhg值")
    @PostMapping("/updateNhgByWellIdDefault")
    public R updateNhgByWellIdDefault(@RequestBody Map<String, Object> map) {
        Integer well_id = (Integer) map.get("well_id");
        String sql = "update \"dc_ylDataDc\" set nhg = 1.023 where well_id = " + well_id;
        mapperUtils.updataBySql(sql);
        return R.ok();
    }

    @Operation(summary = "根据井id修改obg数据")
    @PostMapping("/updateObgByWellId")
    public R updateObgByWellId(@RequestBody Map<String, Object> map) {
        Integer well_id = (Integer) map.get("well_id");
        //海水密度
        Double Swg = map.get("swg") != null ? Double.parseDouble(map.get("swg").toString()) : 1.025;
        //水深
        Double Wd = map.get("wd") != null ? Double.parseDouble(map.get("wd").toString()) : 0.0;
        //补心高
        Double Kb = map.get("kb") != null ? Double.parseDouble(map.get("kb").toString()) : 0.0;
        //Obgmax
        Double Obgmax = map.get("Obgmax") != null ? Double.parseDouble(map.get("Obgmax").toString()) : 0.0;
        //φ0
        Double phi0 = map.get("phi0") != null ? Double.parseDouble(map.get("phi0").toString()) : 0.0;
        //pde
        Double pde = map.get("pde") != null ? Double.parseDouble(map.get("pde").toString()) : 0.0;
        //存放id和新的nhg，方便更新
        List<Map<String, Object>> obgList = new ArrayList<>();
        List<Map<String, Object>> depthList = wellInfoService.getDepthList(well_id);
        if (depthList != null && depthList.size() > 0) {
            for (Map<String, Object> depthMap : depthList) {
                Double depth = (Double) depthMap.get("depth");
                Double nhg = (Double) depthMap.get("nhg");
                Integer id = (Integer) depthMap.get("id");
                //先计算phi
                Double phi = phi0 * Math.exp(-pde * (depth - Wd - Kb));
                //计算obg
                Double obg = (Wd * Swg + Obgmax * (depth - Wd - Kb) - (Obgmax - nhg) * phi * (depth - Wd - Kb - 0)) / (depth - Kb);
                depthMap.put("obg", obg);
                depthMap.put("id", id);
                obgList.add(depthMap);
            }
        }
        //更新obg
        if (obgList != null && obgList.size() > 0) {
            for (Map<String, Object> obgMap : obgList) {
                Integer id = (Integer) obgMap.get("id");
                Double obg = (Double) obgMap.get("obg");
                String sql = "update \"dc_ylDataDc\" set obg = " + obg + " where id = " + id;
                mapperUtils.updataBySql(sql);
            }
        }
        return R.ok();
    }

    @Operation(summary = "根据井id修改井的基本信息V0,A,B")
    @PostMapping("/updateWellInfoByWellId")
    public R updateWellInfoByWellId(@RequestBody Map<String, Object> map) {
        Integer well_id = (Integer) map.get("well_id");
        if (well_id == null) {
            return R.error("井id不能为空");
        }
        //获取V0
        String sqlV0 = "select v0 from \"dc_wellInfo\" where well_id = " + well_id;
        List<Map<String, Object>> V0List = mapperUtils.queryBySql(sqlV0);
        if (V0List.size() == 0) {
            return R.error("V0不存在");
        }
        Double V0 = Double.parseDouble(V0List.get(0).get("v0").toString());
        //获取传过来的参数
        Double depth1 = map.get("depth1") != null ? Double.parseDouble(map.get("depth1").toString()) : 0.0;
        Double ρp1 = map.get("ρp1") != null ? Double.parseDouble(map.get("ρp1").toString()) : 0.0;
        Double depth2 = map.get("depth2") != null ? Double.parseDouble(map.get("depth2").toString()) : 0.0;
        Double ρp2 = map.get("ρp2") != null ? Double.parseDouble(map.get("ρp2").toString()) : 0.0;

        //通过深度查询该深度的obg
        String sql1 = "select dt,obg from \"dc_ylDataDc\" where well_id = " + well_id + " and depth = " + depth1;
        List<Map<String, Object>> obgList1 = mapperUtils.queryBySql(sql1);
        if (obgList1.size() == 0) {
            return R.error("depth1该深度不存在");
        }
        String sql2 = "select dt,obg from \"dc_ylDataDc\" where well_id = " + well_id + " and depth = " + depth2;
        List<Map<String, Object>> obgList2 = mapperUtils.queryBySql(sql1);
        if (obgList2.size() == 0) {
            return R.error("depth2该深度不存在");
        }
        Double obg1 = Double.parseDouble(obgList1.get(0).get("obg").toString());
        Double obg2 = Double.parseDouble(obgList2.get(0).get("obg").toString());
        Double dt1 = Double.parseDouble(obgList1.get(0).get("dt").toString());
        Double dt2 = Double.parseDouble(obgList2.get(0).get("dt").toString());
        //计算速度
        Double V1 = (1000000 / dt1);
        Double V2 = (1000000 / dt2);

        //计算压力1
        Double pp1 = ρp1 * 0.0098 * depth1;
        //计算压力2
        Double pp2 = ρp2 * 0.0098 * depth2;
        //计算有效应力1
        Double σeff1 = (obg1 * depth1) - pp1;
        //计算有效应力2
        Double σeff2 = (obg2 * depth2) - pp2;
        //计算A、B
        Double B = Math.log((V1 - V0) / (V2 - V0)) / Math.log(σeff1 / σeff2);
        Double A = (V1 - V0) / (Math.pow(σeff1, B));
        //更新A、B
        String sqlAB = "update \"dc_wellInfo\" set a = " + A + " , b = " + B + " where well_id = " + well_id;
        mapperUtils.updataBySql(sqlAB);
        return R.ok();
    }
}
